package sisi.invoices;

import java.util.List;

import javax.persistence.EntityManager;
/**
 * SuppinvoicedetailController.java - Controller class for the <i>suppinvoicedetail's database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 06/11/2014
 * 
 */
public class SuppinvoicedetailController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Suppinvoicedetail[] get(Integer idOrder, Integer idSupplier, Integer idSeq) {
		List<Suppinvoicedetail> lista = getList(idOrder, idSupplier, idSeq);
			return lista.toArray(new Suppinvoicedetail[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Suppinvoicedetail> getList(Integer idOrder, Integer idSupplier, Integer idSeq) {

		String cWhere = "";
		cWhere = " WHERE c.order="+idOrder+" AND c.suppid="+idSupplier+" AND c.seq="+idSeq;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Suppinvoicedetail as c " + cWhere 
					+ " order by c.order ");

			List<Suppinvoicedetail> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Suppinvoicedetail> getList(Integer orderNum, Integer recNum) {

		String cWhere = " WHERE c.order="+orderNum+" AND c.recnum="+recNum;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Suppinvoicedetail as c " + cWhere 
					+ " order by c.recnum, c.seq ");

			List<Suppinvoicedetail> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Suppinvoicedetail> getList(Integer orderNum) {

		String cWhere = " WHERE c.order="+orderNum;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Suppinvoicedetail as c " + cWhere 
					+ " order by c.seq ");

			List<Suppinvoicedetail> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	public Suppinvoicedetail getSuppinvoicedetail(String productname, Integer orderNum) {

		String cWhere = " WHERE c.order="+orderNum+" AND c.productname ='"+productname+"' ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Suppinvoicedetail as c " + cWhere ); 

			
			Suppinvoicedetail suppinvoicedetail = new Suppinvoicedetail();
			if (q.getResultList().size()==1){			
				suppinvoicedetail = (Suppinvoicedetail)q.getSingleResult();
			} else if (q.getResultList().size()>1 ) {
				suppinvoicedetail = (Suppinvoicedetail)q.getResultList().get(0);
			}
			return suppinvoicedetail;
		} finally {
			em.close();
		}
	}

	
	public Suppinvoicedetail getSuppinvoicedetail(Integer productid, Integer orderNum) {

		String cWhere = " WHERE c.order="+orderNum+" AND c.productid = "+productid+" ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Suppinvoicedetail as c " + cWhere ); 

			
			Suppinvoicedetail suppinvoicedetail = new Suppinvoicedetail();
			if (q.getResultList().size()==1){			
				suppinvoicedetail = (Suppinvoicedetail)q.getSingleResult();
			} else if (q.getResultList().size()>1 ) {
				suppinvoicedetail = (Suppinvoicedetail)q.getResultList().get(0);
			}
			return suppinvoicedetail;

		} catch (Exception e) {
			return null;
			
		} finally {
			em.close();
		}
	}

	
	public Integer countInvoicesxOrderNumber(Integer orderNumber) {

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select COUNT(c) from Suppinvoicedetail as c WHERE c.order="+orderNumber);
			Object risultato = q.getSingleResult();
			return Integer.valueOf( ((Long)risultato ).intValue() );
		} finally {
			em.close();
		}
	}
	
	public Suppinvoicedetail getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Suppinvoicedetail suppinvoicedetailx = em.find(Suppinvoicedetail.class, id);
			return suppinvoicedetailx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Suppinvoicedetail add(Suppinvoicedetail suppinvoicedetail) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(suppinvoicedetail);
			em.getTransaction().commit();
			suppinvoicedetail = refresh(suppinvoicedetail);
		} finally {
			em.close();
			return suppinvoicedetail;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Suppinvoicedetail suppinvoicedetail) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Suppinvoicedetail suppinvoicedetailx = em.find(Suppinvoicedetail.class, suppinvoicedetail.getIdmaster());
			suppinvoicedetailx.setType(suppinvoicedetail.getType());
			suppinvoicedetailx.setSeq(suppinvoicedetail.getSeq());
			suppinvoicedetailx.setNote(suppinvoicedetail.getNote());
			suppinvoicedetailx.setOrder(suppinvoicedetail.getOrder());
			suppinvoicedetailx.setSuppid(suppinvoicedetail.getSuppid());
			suppinvoicedetailx.setProductid(suppinvoicedetail.getProductid());
			suppinvoicedetailx.setProductname(suppinvoicedetail.getProductname());
			suppinvoicedetailx.setQuantity(suppinvoicedetail.getQuantity());
			suppinvoicedetailx.setUm(suppinvoicedetail.getUm());
			suppinvoicedetailx.setSuppvatperc(suppinvoicedetail.getSuppvatperc());
			suppinvoicedetailx.setSuppvat(suppinvoicedetail.getSuppvat());
			suppinvoicedetailx.setCurr(suppinvoicedetail.getCurr());
			suppinvoicedetailx.setStat(suppinvoicedetail.getStat());
			suppinvoicedetailx.setPartno(suppinvoicedetail.getPartno());
			suppinvoicedetailx.setUnitprice(suppinvoicedetail.getUnitprice());
			suppinvoicedetailx.setRecnum(suppinvoicedetail.getRecnum());
			suppinvoicedetailx.setCustid(suppinvoicedetail.getCustid());
			suppinvoicedetailx.setCustname(suppinvoicedetail.getCustname());
			suppinvoicedetailx.setDtUp(suppinvoicedetail.getDtUp());
			suppinvoicedetailx.setExchangerate(suppinvoicedetail.getExchangerate());
			suppinvoicedetailx.setNfysNote(suppinvoicedetail.getNfysNote());
			suppinvoicedetailx.setPaid(suppinvoicedetail.getPaid());
			suppinvoicedetailx.setPaidamount(suppinvoicedetail.getPaidamount());
			suppinvoicedetailx.setSuppname(suppinvoicedetail.getSuppname());
			suppinvoicedetailx.setSuppnuminvoice(suppinvoicedetail.getSuppnuminvoice());
			suppinvoicedetailx.setTdoc(suppinvoicedetail.getTdoc());
			suppinvoicedetailx.setBillitem(suppinvoicedetail.getBillitem());
			suppinvoicedetailx.setPaiddt(suppinvoicedetail.getPaiddt());
			suppinvoicedetailx.setSupptaxed(suppinvoicedetail.getSupptaxed());
			suppinvoicedetailx.setTotalamount(suppinvoicedetail.getTotalamount());
			suppinvoicedetailx.setVatarticle(suppinvoicedetail.getVatarticle());
			suppinvoicedetailx.setVatcode(suppinvoicedetail.getVatcode());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Suppinvoicedetail refresh(Suppinvoicedetail suppinvoicedetail) {

		Suppinvoicedetail suppinvoicedetailx = suppinvoicedetail;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			suppinvoicedetailx = em.find(Suppinvoicedetail.class, suppinvoicedetail.getIdmaster());
			em.flush();
			em.refresh(suppinvoicedetailx);
		} finally {
			em.close();
			return suppinvoicedetailx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Suppinvoicedetail suppinvoicedetail) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Suppinvoicedetail suppinvoicedetailx = em.find(Suppinvoicedetail.class, suppinvoicedetail.getIdmaster());
			em.remove(suppinvoicedetailx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//
